package com.linq.cool.sdk.command;

import com.linq.cool.qqbot.myqq.annotation.MyQQRobotCommand;
import com.linq.cool.qqbot.myqq.callback.MyQQMessageCallbackRequest;
import com.linq.cool.qqbot.myqq.enums.MyQQTypeEnum;
import com.linq.cool.qqbot.myqq.handler.MyQQRobotCommandHandler;

import com.linq.cool.qqbot.myqq.response.MyQQMessageCallbackResponse;
import com.linq.cool.qqbot.myqq.response.MyQQResponse;
import com.linq.cool.qqbot.myqq.template.MyQQHttpRequestTemplate;
import com.linq.cool.sdk.dto.ItpkReceiveQQMsgDto;
import com.linq.cool.sdk.dto.TuringReceiveQQMsgDto;
import com.linq.cool.sdk.enums.MyQQRobotEnum;
import com.linq.cool.sdk.request.myqq.MyQQApiSendMsgExRequest;
import com.linq.cool.sdk.template.ItpkHttpRequestTemplate;
import com.linq.cool.sdk.template.TuringHttpRequestTemplate;
import com.linq.cool.sdk.utils.MyQQUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author yqlin
 * @date 2021/8/6 10:12
 * @description
 */
@Slf4j
@MyQQRobotCommand(regex = ".*\\[@[1-9][0-9]{4,14}]$")
public class MyQQRobotCommandRobotGroupReplyHandler implements MyQQRobotCommandHandler {
    @Autowired
    private MyQQHttpRequestTemplate myQQHttpRequestTemplate;
    @Autowired
    private ItpkHttpRequestTemplate itpkHttpRequestTemplate;
    @Autowired
    private TuringHttpRequestTemplate turingHttpRequestTemplate;


    @Override
    public MyQQMessageCallbackResponse handle(MyQQMessageCallbackRequest callbackRequest) {
        String message = callbackRequest.getMsg();
        MyQQResponse sendMsgExResponse;
        String targetQQ = MyQQUtil.parseQQ(message);
        log.info("目标QQ: {}", targetQQ);
        if (MyQQRobotEnum.ROT_QI_2303574989.getCode().equals(targetQQ) && MyQQRobotEnum.ROT_QI_2303574989.getCode().equals(callbackRequest.getRobotQQ())) {
            log.info("监听机器人QQ: {}", callbackRequest.getRobotQQ());
            String itpkResponse = itpkHttpRequestTemplate.doRequest(new ItpkReceiveQQMsgDto().setMsg(message));
            sendMsgExResponse = myQQHttpRequestTemplate.doRequest(new MyQQApiSendMsgExRequest()
                                                                          .setInfoType(MyQQTypeEnum.MESSAGE_TYPE_GROUP.getCode())
                                                                          .setGroupQQ(callbackRequest.getFromId())
                                                                          .setRobotQQ(callbackRequest.getRobotQQ())
                                                                          .setContent(itpkResponse), MyQQApiSendMsgExRequest.class);
            if (sendMsgExResponse.getSuccess()) {
                return MyQQMessageCallbackResponse.next();
            }
        } else if (MyQQRobotEnum.ROT_LINQ_3377894260.getCode().equals(targetQQ) && MyQQRobotEnum.ROT_LINQ_3377894260.getCode().equals(callbackRequest.getRobotQQ())) {
            log.info("监听机器人QQ: {}", callbackRequest.getRobotQQ());
            String itpkResponse = turingHttpRequestTemplate.getReplyMessage(new TuringReceiveQQMsgDto().setMsg(message).setQq(callbackRequest.getFromQQ()));
            sendMsgExResponse = myQQHttpRequestTemplate.doRequest(new MyQQApiSendMsgExRequest()
                                                                          .setInfoType(MyQQTypeEnum.MESSAGE_TYPE_GROUP.getCode())
                                                                          .setGroupQQ(callbackRequest.getFromId())
                                                                          .setRobotQQ(callbackRequest.getRobotQQ())
                                                                          .setContent(itpkResponse), MyQQApiSendMsgExRequest.class);
            if (sendMsgExResponse.getSuccess()) {
                return MyQQMessageCallbackResponse.next();
            }
        }
        return MyQQMessageCallbackResponse.intercept();
    }
}
